package com.junzhiit.nengji.cms.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.junzhiit.nengji.bo.AdvertiseBO;
import com.junzhiit.nengji.cms.service.ModulesAdvertiseService;
import com.junzhiit.nengji.cms.web.exception.AdvertiseNotFoundException;
import com.junzhiit.nengji.cms.web.exception.ModulesAdvertiseNotFoundException;
import com.junzhiit.nengji.cms.web.util.ViewObjectUtil;
import com.junzhiit.nengji.cms.web.util.aliyun.AliyunOSSClientUtil;
import com.junzhiit.nengji.cms.web.vo.AdvertiseAddVO;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.dao.AdvertiseDAO;
import com.junzhiit.nengji.dao.ModulesAdvertiseDAO;
import com.junzhiit.nengji.model.Advertise;
import com.junzhiit.nengji.model.ModulesAdvertise;
import com.junzhiit.nengji.model.enums.Status;

@Service
public class ModulesAdvertiseServiceImpl extends BaseServiceImpl implements ModulesAdvertiseService {

	@Autowired
	private ModulesAdvertiseDAO modulesAdvertiseDAO;

	@Autowired
	private AdvertiseDAO advertiseDAO;

	@Value("${upload.temp}")
	private String uploadPath;

	@Value("${upload.mall.advertise}")
	private String advertisePath;
	
	//阿里云API的bucket名称
	@Value("${oss.backet.name}")
	private String backetName;

	@Override
	public Map<String, Object> findAll(DataTablesParamVO dtp, String modulesId) {
		List<AdvertiseBO> list = modulesAdvertiseDAO.findAll(modulesId, dtp.getStart(), dtp.getLength());
		int totalCount = modulesAdvertiseDAO.getTotalCount(modulesId);
		return ViewObjectUtil.dataTables(list, dtp.getDraw(), totalCount);
	}

	@Override
	public ResultVO add(AdvertiseAddVO advertiseAddVO, String modulesId) {

		ResultVO result = new ResultVO();

		Advertise advertise = new Advertise();
		BeanUtils.copyProperties(advertiseAddVO, advertise);
		advertise.setStatus(Status.ACTIVE.toIntStatus());
		Advertise data = advertiseDAO.insert(advertise);

		String ImgUrl = data.getImgUrl();
		if (StringUtils.isNotBlank(ImgUrl)) {
			//FileUtil.moveFile(uploadPath + ImgUrl, advertisePath);
			boolean is = AliyunOSSClientUtil.moveFile(backetName,ImgUrl, advertisePath, uploadPath);
			if(is){
				AliyunOSSClientUtil.removeFile(backetName, uploadPath+ImgUrl);
			}
		}

		ModulesAdvertise modulesAdvertise = new ModulesAdvertise();
		modulesAdvertise.setAdvertiseId(data.getId());
		modulesAdvertise.setModulesId(modulesId);

		modulesAdvertiseDAO.insert(modulesAdvertise);
		return result;
	}

	@Override
	public ResultVO del(String modulesAdvertiseId) {
		ResultVO result = new ResultVO();
		ModulesAdvertise modulesAdvertise = modulesAdvertiseDAO.query(modulesAdvertiseId);
		if (modulesAdvertise == null) {
			throw new ModulesAdvertiseNotFoundException("模块广告未找到");
		}

		String advertiseId = modulesAdvertise.getAdvertiseId();
		Advertise advertise = advertiseDAO.query(advertiseId);
		if (advertise == null) {
			throw new AdvertiseNotFoundException("该广告不存在！");
		}

		advertiseDAO.updateToDelete(advertiseId);
		modulesAdvertiseDAO.updateToDelete(modulesAdvertiseId);

		return result;
	}
}
